import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


export default new Vuex.Store({
  state: {
    drinks:require("@/assets/json/inventory.json").inventory,
    admins:require("@/assets/json/admins.json").admins,
    cart:[],
    balance:200,
    orderList:[],
    salesVolume:0,
    pageName:'自动饮料售卖机',
    btnName:'管理员登陆',
    advertise:require("@/assets/json/advertisement.json").advertise,
    nameRecord:require("@/assets/json/inventory.json").nameRecord
  },
  mutations: {

    //管理员操作
    //添加商品
    addInInventory(state,item){
      state.drinks.push(item)
      console.log(state.drinks)
    },
    //确认更新商品列表
    confirmUpdateList(state,newList){
      state.drinks = newList
      console.log(state.drinks)
    },

    //更新广告位
    updateAdvertise(state,newAdvertises){
      state.advertise = newAdvertises
    },

    //售货机余额更新
    updateBalance(state,income){

    },

    //东西加入购物车
    addInCart(state,item){
      state.cart.push(item)
    },
    refreshCartItem(state,data){
      /* 通过 Vue.set 来做到响应式的更新， */
      Vue.set(state.cart,data.index,data.freshItem)
    },

    //更改点击属性
    changeClicked(state,index){
      state.drinks[index].clicked = false
    },

    //生成新的订单
    newOrder(state,item){
      state.orderList.push(item)

      /* 相应库存量减少 */
      let content = item.get('content')
      for(let i=0;i<content.length;i++){
        state.drinks[content[i].index].remain -= content[i].count
      }

      //计算本次交易结余，然后放入余额中
      /*。。。*/
      console.log(state.orderList)
    }
  },
  actions: {
  },
  modules: {
  }
})
